package net.wds.study.dep.dao.impl;

import net.wds.study.clazz.utils.PageModel;
import net.wds.study.dep.dao.DepartmentDao;
import net.wds.study.dep.entity.Department;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by gf on 2020/9/17.
 */
@Repository("departmentDao")
public class DepartmentDaoImpl implements DepartmentDao {

    @Resource
    private SessionFactory sessionFactory;

    public Session getSession() {
        return sessionFactory.getCurrentSession();
    }


    @Override
    public PageModel<Department> getPage(Integer statIndex, Integer pageSize, String depName, String depManger) {

        PageModel<Department> departmentPageModel = new PageModel<>();
        StringBuilder hql = new StringBuilder("select d from Department d where 1=1 ");
        List<Object> params = new ArrayList();
        if (!("".equals(depName) && depName != null)) {
            hql.append(" and depName like ? ");
            params.add("%"+depName+"%");
        }
        if (!("".equals(depManger) && depManger != null)) {
            hql.append(" and depManger like ? ");
            params.add("%"+depManger+"%");
        }
        Query query = getSession().createQuery(hql.toString());
        for (int i = 0; i < params.size(); i++) {
            query.setParameter(i, params.get(i));
        }
        int totalPageNum = query.list().size();//总条数
        departmentPageModel.setTotal(totalPageNum);
        //计算为总页数
        totalPageNum = totalPageNum%departmentPageModel.getPageSize()==0 ? totalPageNum/departmentPageModel.getPageSize() : (totalPageNum/departmentPageModel.getPageSize() + 1);
        departmentPageModel.setPageTotal(totalPageNum);

        query.setFirstResult(statIndex);
        query.setMaxResults(pageSize);
        departmentPageModel.setPageData(query.list());
        return departmentPageModel;
    }

    @Override
    public boolean saveDep(Department department) {
        try {
            getSession().save(department);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean updateDep(Department department) {
        StringBuilder hql = new StringBuilder("update Department set ");
        hql.append(" depName='" + department.getDepName() + "',")
                .append(" depManger='" + department.getDepManger() + "',")
                .append(" depMajorCount=" + department.getDepMajorCount())
                .append(", depBuildYear='" + department.getDepBuildYear() + "'")
                .append(" where id=" + department.getId());
        Query query = getSession().createQuery(hql.toString());
        int res = query.executeUpdate();
        return res > 0 ? true : false;
    }

    @Override
    public boolean deleteDep(Integer id) {
        StringBuilder hql = new StringBuilder("delete Department where id= ?");
        Query query = getSession().createQuery(hql.toString());
        query.setParameter(0, id);
        int res = query.executeUpdate();
        return res > 0 ? true : false;
    }
}
